# -*- coding: utf-8 -*-
# models
# models
from openerp import models, api, fields
import openerp.addons.decimal_precision as dp
import datetime

# product_qk = None

class plan_order(models.Model):
    _name= 'plan.order'

    apply_id=fields.Many2one('res.users',string=u"业务员",default=lambda self: self.env.user,readonly=True)

    plan_year=fields.Char(default=lambda self: datetime.datetime.now().strftime('%Y'),string=u"年")
    plan_month=fields.Selection([
            ('1', u'1月'),
            ('2',u'2月'),
            ('3', u'3月'),
            ('4', u'4月'),
            ('5',u'5月'),
            ('6', u'6月'),
            ('7', u'7月'),
            ('8',u'8月'),
            ('9', u'9月'),
            ('10', u'10月'),
            ('11',u'11月'),
            ('12', u'12月'),
            ],
            default=lambda self: str(datetime.datetime.now().month),
            string=u"月")
    plan_order_ids=fields.One2many('plan.order.line','plan_id',string=u"产品预估量明细")
    #鲍成翔修改
    
    user_manager=fields.Many2one('res.users',string=u"当前用户的部门经理",store=True)
    user_manager2=fields.Many2one('res.users',string=u"当前用户的上级部门经理",store=True)
    user_manager3=fields.Many2one('res.users',string=u"当前用户的上级部门副经理",store=True)

    @api.onchange('apply_id')
    def _onchange_apply_id(self):
        if self.apply_id and self.apply_id.employee_ids[0]:
            self.user_manager = self.apply_id.employee_ids[0].department_id.manager_id.user_id
            if self.apply_id.employee_ids[0].department_id.parent_id:
                self.user_manager2=self.apply_id.employee_ids[0].department_id.parent_id.manager_id.user_id
                self.user_manager3 = self.apply_id.employee_ids[0].department_id.parent_id.fumanager_id.user_id
#！！！！！！！！！！！！！！！！！！

    # @api.model
    # def default_get(self, fields):

    #     res = super(sale_order, self).default_get(fields)
    #     res.update({

    #         })
    #     # 
    #     default_order_product_type = self.env.context.get('default_order_product_type', res.get('order_product_type',False))
    #     if default_order_product_type:
    #         res.update({
    #             'order_product_type':default_order_product_type
    #         })
    #     return res

class plan_line(models.Model):
    _name='plan.order.line'

    @api.model
    def _product_qk(self):
        # if product_qk:
        #     return product_qk
        # else:
        print('_product_qk',self)
        product_qks = self.env['product.template'].search([('name','ilike',u'砌块')],limit=1)
        if product_qks:
            print(product_qks.id)
            return product_qks.id
            
    user_manager=fields.Many2one('res.users',string=u"当前用户的部门经理",store=True)
    user_manager2=fields.Many2one('res.users',string=u"当前用户的上级部门经理",store=True)
     


    plan_id=fields.Many2one('plan.order',ondelete='cascade',string=u"每月预估量统计")
    product_tpl_id = fields.Many2one(comodel_name='product.template',string=u'产品名称',default=_product_qk)
    possible_values_level = fields.Many2many(comodel_name='product.attribute.value',
        compute='_get_possible_attribute_values_level')
    product_level = fields.Many2one(comodel_name='product.attribute.value',string=u'级别',domain="[('id', 'in', possible_values_level[0][2])]")
    possible_values_spec = fields.Many2many(comodel_name='product.attribute.value',
        compute='_get_possible_attribute_values_spec')
    product_spec = fields.Many2one(comodel_name='product.attribute.value',string=u'规格',domain="[('id', 'in', possible_values_spec[0][2])]")
    possible_values_hole = fields.Many2many(comodel_name='product.attribute.value',
        compute='_get_possible_attribute_values_hole')
    product_hole = fields.Many2one(comodel_name='product.attribute.value',string=u'手持孔',domain="[('id', 'in', possible_values_hole[0][2])]")
    need_qty=fields.Float(string=u"本月预估量")
    infor=fields.Char(string=u"备注")
    plan_year_id=fields.Char(string=u"年",related='plan_id.plan_year',store=True)
    plan_month_id=fields.Selection([
            ('1', u'1月'),
            ('2',u'2月'),
            ('3', u'3月'),
            ('4', u'4月'),
            ('5',u'5月'),
            ('6', u'6月'),
            ('7', u'7月'),
            ('8',u'8月'),
            ('9', u'9月'),
            ('10', u'10月'),
            ('11',u'11月'),
            ('12', u'12月'),
            ],string=u"月",related='plan_id.plan_month',store=True)
    submit_id=fields.Many2one('res.users',string=u"业务员",related='plan_id.apply_id',store=True)

    @api.one
    @api.depends('product_tpl_id',
                 'product_tpl_id.attribute_line_ids')
    def _get_possible_attribute_values_level(self):
        print('_get_possible_attribute_values_level',self)
        attr_values = self.env['product.attribute.value']
        index = 0
        for attr_line in self.product_tpl_id.attribute_line_ids:
            if index == 1:
                attr_values |= attr_line.value_ids
            index +=1
        self.possible_values_level = attr_values.sorted()
        print(self.possible_values_level)

    @api.one
    @api.depends('product_tpl_id',
                 'product_tpl_id.attribute_line_ids')
    def _get_possible_attribute_values_spec(self):
        print('_get_possible_attribute_values_spec',self)
        attr_values = self.env['product.attribute.value']
        index = 0
        for attr_line in self.product_tpl_id.attribute_line_ids:
            if index == 0:
                attr_values |= attr_line.value_ids
            index +=1
        self.possible_values_spec = attr_values.sorted()
        print(self.possible_values_spec)

    @api.one
    @api.depends('product_tpl_id',
                 'product_tpl_id.attribute_line_ids')
    def _get_possible_attribute_values_hole(self):
        print('_get_possible_attribute_values_hole',self)
        attr_values = self.env['product.attribute.value']
        index = 0
        for attr_line in self.product_tpl_id.attribute_line_ids:
            if index == 2:
                attr_values |= attr_line.value_ids
            index +=1
        self.possible_values_hole = attr_values.sorted()
        print(self.possible_values_hole)



    